<?php

namespace Api\Controller;

use Common\Util\Version;

class VersionController extends BaseController {
    
    /**
     * 版本检查
     * @param platform ios、android
     * @param version 1.0.1
     */ 
    public function check()
    {
        $platform = I('platform',0);
        $version = I('version',0);

        if( !$platform )
        {
            $platform = ($_SERVER['HTTP_PLATFORM']) ? $_SERVER['HTTP_PLATFORM'] : 0;
        }

        if( !$version )
        {
            $version = ($_SERVER['HTTP_VERSION']) ? $_SERVER['HTTP_VERSION'] : 0;
        }

        if(empty($platform) || empty($version))
        {
            $this->returnInfo(-1,"参数错误",[
                "platform" => $platform,
                "version" => $version,
            ]);
        }

        if( 1 === (int)$platform || preg_match("/ios/i",$platform) )
        {
            $platform = 1;
        }else{
            $platform = 2;
        }

        $Version_std = new Version;
        
        $res = $Version_std->app_ver_chk($version,$platform);

        if( !$res )
        {
            #没有新版
            $this->returnInfo(0,"您的应用版本已是最新",[
                "platform" => $platform,
                "version" => $version,
            ]);
        }

        $this->returnInfo(1,"有新的版本可以更新",$res);

    }

    public function getNewest()
    {
        $platform = I('platform',0); 

        if( !$platform )
        {
            $platform = ($_SERVER['HTTP_PLATFORM']) ? $_SERVER['HTTP_PLATFORM'] : 0;
        } 
        if(empty($platform))
        {
            $this->returnInfo(-1,"参数错误",[
                "platform" => $platform, 
            ]);
        }

        if( 1 === (int)$platform || preg_match("/ios/i",$platform) )
        {
            $platform = 1;
        }else{
            $platform = 2;
        }

        $new = M("app_version")->where([
            "platform" => $platform,
            "status" => 1,
        ])->order("app_ver_num desc,app_ver_code desc")
        ->field("id,app_ver,app_ver_num,app_ver_code,app_ver_name,last_need_update_ver,update_tissue,update_desc,download_url")->find();
        
        if( !$new )
        {
            $this->returnInfo(0,"您的应用版本已是最新",[
                "platform" => $platform,
                "version" => $version,
            ]);
        }

        $new['new_version_code'] =  $new['app_ver_code'] ? $new['app_ver_code'] : M("app_version")->where([
            "platform" => $platform,
            "status" => 1,
        ])->count();

        unset( $new['app_ver_code'] );

        $new['for_update'] = $new['last_need_update_ver'] < $new['app_ver_num'] ? true :false;

        $this->returnInfo(1,"获取成功",$new);

    }
    
}